package com.example.battle.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import com.example.battle.R;
import com.example.battle.entity.User;
import com.example.battle.util.ActivityCollector;
import com.example.battle.widget.TitleLayout;


public class EditName extends AppCompatActivity {
    private TitleLayout tl_title;
    private EditText edit_name;
    private User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityCollector.addActivity(this);
        setContentView(R.layout.activity_edit_name);

        tl_title = (TitleLayout) findViewById(R.id.tl_title);
        edit_name = (EditText) findViewById(R.id.et_edit_name);

        Intent intent = getIntent();
        user = (User) intent.getSerializableExtra("user");
        edit_name.setText(user.getUserName());
        //设置监听器
        //如果点击完成
        tl_title.getTextView_forward().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                user.setUserName(edit_name.getText().toString());
                Login.getInstance().getAppDB().battleDao().updateUserInfo(user);
                Intent intent1 = new Intent(EditName.this,PersonInfo.class);
                intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent1.putExtra("user",user);
                startActivity(intent1);
                finish();
            }
        });
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }
}
